package cn.aukw.todolist.adapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.view.LayoutInflater;


import cn.aukw.todolist.MainActivity;
import cn.aukw.todolist.bean.ListBean;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import cn.aukw.todolist.R;
import cn.aukw.todolist.customviews.SmoothCheckBox;


public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> {
    private List<ListBean> list = new ArrayList<>();
    private Context mContext;
    private ItemClickListener itemClickListener;
    private OnTaskCompletedListener taskCompletedListener;


    public ListAdapter(Context context, ItemClickListener itemClickListener, OnTaskCompletedListener taskCompletedListener) {
        this.mContext = context;
        this.itemClickListener = itemClickListener;
        this.taskCompletedListener = taskCompletedListener;
    }

    public void setData(List<ListBean> list) {
        if (list != null) {
            this.list = list;
        } else {
            this.list.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.list_item,
                parent, false);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ListBean bean = list.get(position);
        if (bean == null) return;

        // 绑定数据到试图
        holder.tv_title.setText(bean.getTitle());
        holder.tv_content.setText(bean.getContent());
        holder.tv_time.setText(bean.getTime());

        // 绑定数据到视图
        holder.tv_title.setText(bean.getTitle());
        holder.tv_content.setText(bean.getContent());
        holder.tv_time.setText(bean.getTime());

        // 清除多余的监听器，避免重复回调
        holder.cb_completed.setOnCheckedChangeListener(null);

        // 设置任务完成状态
        holder.cb_completed.setChecked(bean.isCompleted());
        if (bean.isCompleted()) {
            holder.tv_title.setPaintFlags(holder.tv_title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_content.setPaintFlags(holder.tv_content.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.gray));
        } else {
            holder.tv_title.setPaintFlags(holder.tv_title.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_content.setPaintFlags(holder.tv_content.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.black));
        }

        // 设置完成事件监听器
        holder.cb_completed.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (taskCompletedListener != null) {
                bean.setCompleted(isChecked);
                taskCompletedListener.onTaskCompleted(bean.getId(), isChecked);
                if (isChecked) {
                    ((MainActivity) mContext).playSound(((MainActivity) mContext).soundComplete);
                }
            }
        });


        holder.bindClickListener(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_title;
        public TextView tv_content;
        public TextView tv_time;
        public SmoothCheckBox cb_completed;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_time = itemView.findViewById(R.id.tv_time);
            cb_completed = itemView.findViewById(R.id.cb_completed);
        }

        public void bindClickListener(int position) {
            itemView.setOnClickListener(v -> itemClickListener.onItemClick(itemView, position));

            itemView.setOnLongClickListener(v -> {
                itemClickListener.onItemLongClick(itemView, position);
                return true;
            });
        }
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public interface OnTaskCompletedListener {
        void onTaskCompleted(int taskId, boolean isCompleted);

    }

}
